<?php
/**
 * 数据库单表操作基础类
 */
class operate
{
    protected $db;
    protected $table;
    protected $condtion;
    public $error;
    public $pages;
    public $pageinfo;
    public $db_type = 'master';

    public function __construct()
    {
        global $db;
        $this->db = &$db;
        $this->condtion = array();
    }

    /**
     * 按条件查询数据并分页
     * @author menghao
     * @param <array> $condition 查询条件
     * @param <string> $order 排序order by XX
     * @param <int> $page 当前页
     * @param <int> $pagesize 分页大小
     * @return <array>
     */
    public function listinfo($condition, $order = '', $page=1, $pagesize=20)
    {
        $where = 'where status=?';
        $this->add_condition('int', STATUS_NORMAL, '=');

        if(isset($condition) && is_array($condition))
        {
            foreach($condition as $key=>$val)
            {
                $where .= " and $key=?";
                $this->add_condition('string', $val);
            }
        }
        return $this->query("select * from $this->table $where $order", $page, $pagesize);
    }

    /**
     * 按id 获取一条记录
     * @author menghao
     * @param <string> $id 主键
     * @return <array>
     */
    public function get_one($id)
    {
        return $this->db->get_one_normal($this->table, $id, 'id', $this->db_type);
    }

    /**
     * 插入数据
     * @author menghao
     * @param <array> $data
     * @return <boolean>
     */
    public function add($data)
    {
        return $this->db->insert($this->table, $data);
    }

    /**
     * 按id更新数据
     * @author menghao
     * @param <string> $id
     * @param <array> $data
     * @return <boolean>
     */
    public function edit($id, $data)
    {
        return $this->db->simple_update($this->table, $data, array('id'=>$id));
    }

    /**
     * 按id删除数据 逻辑删除
     * @author menghao
     * @param <string> $id
     * @return <boolean>
     */
    public function delete($id)
    {
        return $this->edit($id, array('status'=>STATUS_DEL));
    }

    /**
     * 添加查询条件
     * @author menghao
     * @param <string> $type 数据类型
     * @param <string> $condition 条件
     */
    protected function add_condition($type, $condition)
    {
        array_push(&$this->condtion, array($type,$condition));
    }

    /**
     * 按以添加的查询条件进行设置
     * @author menghao
     * @return <boolean>
     */
    protected function set_condition()
    {
        if(!isset($this->condtion) || !is_array($this->condtion))
        {
            return false;
        }
        foreach($this->condtion as $c)
        {
            $fun = "set_".$c[0];
            $this->db->$fun($c[1]);
        }
    }


    protected function clear_condition()
    {
        $this->condtion = array();
    }

    /**
     * 查询数据并分页
     * @author menghao
     * @param <string> $sql
     * @param <int> $page
     * @param <int> $pagesize
     * @return <array>
     */
    protected function query($sql, $page=null, $pagesize=null, $page_mode=null)
    {
        if(isset($page) && isset($pagesize))
        {
            $page = max(intval($page), 1);
            $offset = ($page - 1) * $pagesize;

            $sql_count = "select count(*) count ".strstr($sql, 'from');
            $this->db->prepare($sql_count, $this->db_type);
            $this->set_condition();
            $result = $this->db->execute(true);
            $count = array_pop(&$result);

            $this->pageinfo = array('count' => $count, 'page' => $page, 'pagesize' => $pagesize);
            if( !empty($page_mode) )
            {
                $this->pages = pages($count, $page, $pagesize, 5);
            }
            else
            {
                $this->pages = pages($count, $page, $pagesize);
            }
        }
        if(isset($page) && isset($pagesize))
        {
            $sql .= ' limit ?,?';
        }
        $this->db->prepare($sql, $this->db_type);
        if(isset($page) && isset($pagesize))
        {
            $this->add_condition('int', $offset);
            $this->add_condition('int', $pagesize);
        }
        $this->set_condition();
        $this->clear_condition();
        return $result = $this->db->execute();
    }

    /**
     * 查询所有数据
     * @author menghao
     * @param <string> $sql
     * @return <array>
     */
    protected function query_all($sql)
    {
        $this->db->prepare($sql, $this->db_type);
        $this->set_condition();
        $this->clear_condition();
        return $result = $this->db->execute();
    }
    /**
     * 按id排序
     * @author menghao
     * @param <array> $listorder
     * @return <boolean>
     */
    public function listorder($listorder)
    {
        if(!is_array($listorder)) return false;
        foreach($listorder as $key=>$val)
        {
            $this->db->simple_update($this->table, array('listorder'=>$val), array('id'=>$key));
        }
        return true;
    }

}
?>
